package net.lunjin.controller;

import java.math.BigDecimal;
import java.util.List;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.lunjin.bean.News;
import net.lunjin.bean.NewsRemark;
import net.lunjin.bean.Organizations;
import net.lunjin.service.NewsRemarkService;
import net.lunjin.service.NewsService;
import net.lunjin.service.OrganizationsService;
import net.lunjin.util.Constants;
import net.lunjin.util.ProjectUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import finaler.util.DateUtil;
import finaler.util.Page;
import finaler.util.PageList;

@Controller
@RequestMapping("news")
public class NewsController {

	@Autowired
	private NewsService newsService;
	
	@Autowired
	private NewsRemarkService newsRemarkService;
	
	@Autowired
	private OrganizationsService organizationsService;
	
	/**
	 * 新闻列表
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping( value="/type_{newsType}/" )
	public String list(@PathVariable int newsType,HttpServletRequest request) throws Exception {
		request.setAttribute("newsType", newsType);

		List<News> bbsNewsList = newsService.getBbsTopRecord(10);//10条交流新贴
		request.setAttribute("bbsNewsList", bbsNewsList);

		List<News> fmNewsList = newsService.getRandomRecord(0, 10);//随机10条负面新闻
		request.setAttribute("fmNewsList", fmNewsList);
		
		Page page = new Page(request);
		page.setRange(10);
		page.putParameter("newsType", newsType);
		page.putParameter("searchkey", request.getParameter("searchkey"));
		PageList newsList = newsService.getNewsList(page);//新闻查询
		request.setAttribute("newsList", newsList);
		return "/news/list";
	}

	/**
	 * 新闻详细
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping( value="/{newsId}.html" )
	public String detail(@PathVariable BigDecimal newsId, HttpServletRequest request) throws Exception {
		News newsDetail = newsService.findByKey(newsId);//新闻查询
		if(newsDetail==null){
			return "redirect:"+Constants.URL;
		}
		request.setAttribute("newsDetail", newsDetail);
		
		News updateNews = new News();
		updateNews.setId(newsId);
		updateNews.setHit(newsDetail.getHit( ).add(new BigDecimal(1)));
		newsService.update(updateNews);
		
		List<News> beforeAfterList = newsService.getBeforeAfterList(newsId,newsDetail.getNewstype());
		request.setAttribute("beforeAfterList", beforeAfterList);
		
		Page page = new Page(request);
		page.setRange(10);
		page.putParameter("newsId", newsId);
		PageList newsRemarkList = newsRemarkService.getNewsRemarkList(page);
		request.setAttribute("newsRemarkList", newsRemarkList);

		List<News> bbsNewsList = newsService.getBbsTopRecord(10);//10条交流新贴
		request.setAttribute("bbsNewsList", bbsNewsList);

		List<News> fmNewsList = newsService.getRandomRecord(0, 15);//随机15条负面新闻
		request.setAttribute("fmNewsList", fmNewsList);
		
		List<Organizations> randomComList = organizationsService.getRandomComList(40);
		request.setAttribute("randomComList", randomComList);
		
		int a = new Random().nextInt(10);
		int b = new Random().nextInt(10);
		int c = a  + b;
		request.setAttribute("ns", a+"+"+b);
		request.getSession().setAttribute("nc", c);
		
		return "/news/newsDetail";
	}
	
	@RequestMapping( value="/remark",method=RequestMethod.POST )
	public String remark(HttpServletRequest request,HttpServletResponse response) throws Exception {

		if(request.getSession().getAttribute("nc")==null ||
				!request.getSession().getAttribute("nc").toString().equals(request.getParameter("nc"))){//防外部提交
			response.setContentType("text/html; charset=utf-8");
			response.getWriter().print("<script>alert('验证码不正确!请重新输入!');window.history.go(-1);</script>");
			return null;
		}
		
		NewsRemark newsRemark = new NewsRemark();
		newsRemark.setRmcontent(request.getParameter("rmcontent").replaceAll("<", "＜").replaceAll("\n", "<br/>"));//内容
		newsRemark.setAutoflag(new BigDecimal(0));//正常发
		newsRemark.setNewsid(new BigDecimal(request.getParameter("newsId")));
		newsRemark.setCreatetime(DateUtil.getCurrentDate("yyyy-MM-dd HH:mm:ss"));
		newsRemark.setCreateuser(request.getRemoteAddr());
		newsRemarkService.insert(newsRemark);
		return "redirect:/news/"+request.getParameter("newsId")+".html#remarkDiv"; 
	}

	@RequestMapping( value="/jynews.html" )
	public String jynews(HttpServletRequest request) throws Exception {
		
		List<News> bbsNewsList = newsService.getBbsTopRecord(10);//10条交流新贴
		request.setAttribute("bbsNewsList", bbsNewsList);

		List<News> fmNewsList = newsService.getRandomRecord(0, 15);//随机15条负面新闻
		request.setAttribute("fmNewsList", fmNewsList);
		
		return "/news/jynewsList"; 
	}
	/**
	 * 准备作废方法
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping( value="/jynewsDetail_{date}/{time}.htm" )
	public String jynewsDetail(@PathVariable String date,@PathVariable String time,
			HttpServletRequest request) throws Exception {
		String url = "http://finance.qq.com/a/" + date +"/" +time +".htm";
		String webContentString  = ProjectUtil.getWebSource(url, "gb2312");
		String reg = "<h1>(.+)</h1>(.+)<span class=\"pubTime article-time\">(.+)</span><span class=\"bor-tit\"></span>(.+)<div id=\"Cnt-Main-Article-QQ\" bossZone=\"content\">(.+)<span style=\"";
		Pattern pt = Pattern.compile(reg);
		Matcher mc= pt.matcher(webContentString); 
		if(mc.find()){
			request.setAttribute("title", mc.group(1));
			request.setAttribute("pubtime", mc.group(3));
			request.setAttribute("content", "<div>"+mc.group(5).replaceAll("<a", "<noa"));
		}
		List<News> bbsNewsList = newsService.getBbsTopRecord(10);//10条交流新贴
		request.setAttribute("bbsNewsList", bbsNewsList);

		List<News> fmNewsList = newsService.getRandomRecord(0, 15);//随机15条负面新闻
		request.setAttribute("fmNewsList", fmNewsList);
		
		List<Organizations> randomComList = organizationsService.getRandomComList(40);
		request.setAttribute("randomComList", randomComList);
		
		return "/news/jynewsDetail";
	}
	
}
